1
本番環境への移行:デプロイメントのマインドセット
EvoClass-AI002Lecture 10
00:00

本番環境への移行:デプロイメントのマインドセット

この最終モジュールは、ノートブックで高い精度を達成した成功した研究と、信頼できる実行の間にあるギャップを埋めます。デプロイメントとは、PyTorchモデルを最小限の、 自己完結型サービス エンドユーザーに低レイテンシで効率的に予測を提供できる 高可用性というプロセスです。

1. 本番環境向けのマインドセットの転換

Jupyterノートブックの探索的環境は、本番環境では状態依存的かつ脆弱です。私たちは、探索的なスクリプトから、同時にリクエストを処理し、リソース最適化を行い、大きなシステムとのシームレスな統合が可能な構造的でモジュール型のコンポーネントへとコードを再設計しなければなりません。

低レイテンシ推論: ターゲットのしきい値(例:$50\text{ms}$)以下で一貫した予測時間の達成。リアルタイムアプリケーションにとって不可欠です。
高可用性: 信頼性があり、状態なしで、障害からの迅速な回復が可能なようにサービスを設計すること。
再現性: デプロイされたモデルと環境(依存関係、重み、設定)が検証済みの研究結果と正確に一致することを保証すること。
注目点:モデルサービス
学習スクリプト全体をデプロイする代わりに、最小限の自己完結型サービスラッパーをデプロイします。このサービスは以下の3つのタスクのみを処理する必要があります:最適化されたモデルアーティファクトの読み込み、入力の前処理の適用、順伝播の実行による予測の返却。
inference_service.py
TERMINALbash — uvicorn-service
> Ready. Click "Simulate Deployment Flow" to run.
>
ARTIFACT INSPECTOR Live

Simulate flow to view loaded production artifacts.
Question 1
Which feature of a Jupyter notebook makes it unsuitable for production deployment?
It primarily uses Python code
It is inherently stateful and resource-intensive
It cannot directly access the GPU
Question 2
What is the primary purpose of converting a PyTorch model to TorchScript or ONNX before deployment?
Optimization for faster C++ execution and reduced Python dependency
To prevent model theft or reverse engineering
To automatically handle input data preprocessing
Question 3
When designing a production API, when should the model weights be loaded?
Once, when the service initializes
At the start of every prediction request
When the first request to the service is received
Challenge: Defining the Minimal Service
Plan the structural requirements for a low-latency service.
You need to deploy a complex image classification model ($1\text{GB}$) that requires specialized image preprocessing. It must handle $50$ requests per second.
Step 1
To ensure high throughput and low average latency, what is the single most critical structural change needed for the Python script?
Solution:
Refactor the codebase into isolated modules (Preprocessing, Model Definition, Inference Runner) and ensure the entire process is packaged for containerization.
Step 2
What is the minimum necessary "artifact" to ship, besides the trained weights?
Solution:
The exact code/class definition used for preprocessing and the model architecture definition, serialized and coupled with the weights.